package com.example.newbiechen.ireader.presenter;

import com.example.newbiechen.ireader.model.bean.BookChapterBean;
import com.example.newbiechen.ireader.model.bean.CollBookBean;
import com.example.newbiechen.ireader.model.local.BookRepository;
import com.example.newbiechen.ireader.service.boluoxs.BookChapterFinish;
//import com.example.newbiechen.ireader.service.boluoxs.BookChapterService;
import com.example.newbiechen.ireader.service.boluoxs.BookDetailService;
import com.example.newbiechen.ireader.service.boluoxs.BookService;
import com.example.newbiechen.ireader.service.boluoxs.DownloadFinish;
import com.example.newbiechen.ireader.utils.MD5Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by robrk on 2019/3/10.
 */

public class BookDetail2Presenter {

    public CollBookBean getBook(){
        String url = "https://m.boluoxs.com/book/188.html";
//        CollBookBean collBookBean = new CollBookBean(
//                MD5Utils.strToMd5By16(url),
//                "全职法师(boluoxs)",
//                "乱",
//                "　　一觉醒来，世界大变。\\r\\n　　熟悉的高中传授的是魔法，告诉大家要成为一名出色的魔法师。\\r\\n　　居住的都市之外游荡着袭击人类的魔物妖兽，虎视眈眈。\\r\\n　　崇尚科学的世界变成了崇尚魔法，偏偏有着一样以学渣看待自己的老师，一样目光异样的同学，一样社会底层挣扎的爸爸，一样纯美却不能走路的非血缘妹妹……\\r\\n　　不过，莫凡发现绝大多数人都只能够主修一系魔法，自己却是全系全能法师",
//                "/agent/http%3A%2F%2Fimg.1391.com%2Fapi%2Fv1%2Fbookcenter%2Fcover%2F1%2F824066%2F824066_6114388004ba478c8ab3bce0064521fe.jpg%2F",
//                true,
//                6800,
//                63d,
//                "2019-03-09T20:26:25.348Z",
//                null,
//                0,
//                "正文卷 第2727章 这个男人好强",
//                true,
//                false
//        );
        CollBookBean collBookBean = new CollBookBean();
        collBookBean.set_id(MD5Utils.strToMd5By16(url));
        collBookBean.setTitle("全职法师(boluoxs)");
        collBookBean.setAuthor("乱");
        collBookBean.setShortIntro("　　一觉醒来，世界大变。\\r\\n　　熟悉的高中传授的是魔法，告诉大家要成为一名出色的魔法师。\\r\\n　　居住的都市之外游荡着袭击人类的魔物妖兽，虎视眈眈。\\r\\n　　崇尚科学的世界变成了崇尚魔法，偏偏有着一样以学渣看待自己的老师，一样目光异样的同学，一样社会底层挣扎的爸爸，一样纯美却不能走路的非血缘妹妹……\\r\\n　　不过，莫凡发现绝大多数人都只能够主修一系魔法，自己却是全系全能法师");
        collBookBean.setCover("/agent/http%3A%2F%2Fimg.1391.com%2Fapi%2Fv1%2Fbookcenter%2Fcover%2F1%2F824066%2F824066_6114388004ba478c8ab3bce0064521fe.jpg%2F");
        collBookBean.setHasCp(true);
        collBookBean.setLatelyFollower(6800);
        collBookBean.setRetentionRatio(63d);
        collBookBean.setUpdated("2019-03-09T20:26:25.348Z");
        collBookBean.setLastRead("");
//        collBookBean.setChaptersCount(0);
//        collBookBean.setLastChapter("");
        collBookBean.setIsLocal(false);

        collBookBean.setBookSrc(1);
        collBookBean.setChapterUrl("/0/188/");
        return collBookBean;
    }

    public CollBookBean getBook2(){
        String url = "https://m.boluoxs.com/book/340.html";

        CollBookBean collBookBean = new CollBookBean();
        collBookBean.set_id(MD5Utils.strToMd5By16(url));
        collBookBean.setTitle("斗罗大陆IV终极斗罗(boluoxs)");
        collBookBean.setAuthor("唐家三少");
        collBookBean.setShortIntro("　");

        collBookBean.setCover("/agent/https%3A%2F%2Fwww.boluoxs.com%2Ffiles%2Farticle%2Fimage%2F0%2F340%2F340s.jpg");
        collBookBean.setHasCp(true);
        collBookBean.setLatelyFollower(6800);
        collBookBean.setRetentionRatio(63d);
        collBookBean.setUpdated("2019-03-09T20:26:25.348Z");
        collBookBean.setLastRead("");
//        collBookBean.setChaptersCount(0);
//        collBookBean.setLastChapter("");
        collBookBean.setIsLocal(false);

        collBookBean.setBookSrc(1);
        collBookBean.setChapterUrl("/0/340/");
        return collBookBean;
    }

    public void addToBookShelf(String bookId){
//        Runnable runnable = new Runnable() {
//            @Override
//            public void run() {
                try{
                    String url = "https://m.boluoxs.com/book/" + bookId + ".html";
                    CollBookBean collBookBean = BookService.getByUrlAsync(url);
                    collBookBean.setBookId(bookId);
                    collBookBean.setBookSrc(1);
//                collBookBean.setDetailUrl(url);
                    collBookBean.setChapterUrl("/0/" + bookId + "/");

                    collBookBean.setHasCp(true);
                    collBookBean.setLastRead("");
                    collBookBean.setIsLocal(false);

                    List<BookChapterBean> chapterBeans = BookService.getChapters(collBookBean.getChapterUrl(), collBookBean.getBookId());
                    //设置目录
                    collBookBean.setBookChapters(chapterBeans);

                    collBookBean.setChaptersCount(chapterBeans.size());
//                collBookBean.setLastChapter(chapterBeans.get(chapterBeans.size()-1).getTitle());

                    //存储收藏
                    BookRepository.getInstance()
                            .saveCollBookWithAsync(collBookBean);
//                    this.addToBookShelf(collBookBean);
                }catch (Exception ex){
                    ex.printStackTrace();
                }
//            }
//        };
//
//        new Thread(runnable).start();





//        BookDetailService bookDetailService = new BookDetailService(url, new DownloadFinish<BookDetail2Presenter, CollBookBean>(this) {
//            @Override
//            public void onFinish(CollBookBean collBookBean) {
//                collBookBean.setBookId(bookId);
//                collBookBean.setBookSrc(1);
////                collBookBean.setDetailUrl(url);
//                collBookBean.setChapterUrl("/0/" + bookId + "/");
//
//                collBookBean.setHasCp(true);
//                collBookBean.setLastRead("");
//                collBookBean.setIsLocal(false);
//                this.getHandler().addToBookShelf(collBookBean);
//            }
//        });
//        bookDetailService.download();
    }

    private void addToBookShelf(CollBookBean collBookBean) throws Exception {
//        List<BookChapterBean> beans = new ArrayList<>();

        List<BookChapterBean> chapterBeans = BookService.getChapters(collBookBean.getChapterUrl(), collBookBean.getBookId());
        //设置目录
        collBookBean.setBookChapters(chapterBeans);

        collBookBean.setChaptersCount(chapterBeans.size());
//                collBookBean.setLastChapter(chapterBeans.get(chapterBeans.size()-1).getTitle());

        //存储收藏
        BookRepository.getInstance()
                .saveCollBookWithAsync(collBookBean);


//        //获取所有章节
//        BookChapterService bookChapterService = new BookChapterService(collBookBean.getChapterUrl(), new BookChapterFinish() {
//            @Override
//            public void onFinish(List<BookChapterBean> chapterBeans) {
//                //设置目录
//                collBookBean.setBookChapters(chapterBeans);
//
//                collBookBean.setChaptersCount(chapterBeans.size());
////                collBookBean.setLastChapter(chapterBeans.get(chapterBeans.size()-1).getTitle());
//
//                //存储收藏
//                BookRepository.getInstance()
//                        .saveCollBookWithAsync(collBookBean);
//
//            }
//        });
//        bookChapterService.download();
//        //设置 id
//        for(BookChapterBean bean :beans){
//            bean.setId(MD5Utils.strToMd5By16(bean.getLink()));
//        }

//        //设置目录
//        collBookBean.setBookChapters(beans);
//        //存储收藏
//        BookRepository.getInstance()
//                .saveCollBookWithAsync(collBookBean);
    }
}
